package org.example.ychdp.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.example.ychdp.dao.CommentDao;
import org.example.ychdp.entity.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class CommentService {

    @Autowired
    private CommentDao commentDao;
    @Autowired
    private UserService userService;

    public List<Comment> selectAllComment() {
        return commentDao.selectAllComment();
    }

    public int deleteCommentById(Integer concertId) {
        return commentDao.deleteByPrimaryKey(concertId);
    }

    public PageInfo<Comment> searchComment(Integer page) {
        PageHelper.startPage(page, 8);
        CommentExample commentExample = new CommentExample();
        CommentExample.Criteria criteria = commentExample.createCriteria();

        List<Comment> commentList = commentDao.selectByExample(commentExample);
        PageInfo<Comment> pageInfo = new PageInfo<>(commentList);
        return pageInfo;
    }

    public List<Comment> getCommentsByConcerId(Integer id) {
        List<Comment> commentList = commentDao.selectByConcertId(id);
        if (commentList.size() > 0) {
            commentList.stream().map(comment -> {
                User user = userService.selectByPrimaryKey(comment.getUserid());
                comment.setUsername(user.getUsername());

                return comment;
            }).collect(Collectors.toList());
        }
        return commentList;
    }
}
